package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);
        dishDTO.setId(dish.getId());
        dishFlavorsMapper.saveFlavors(dishDTO);
    }

    @Override
    public PageResult selectByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<DishVO> list = dishMapper.selectByPage(dishPageQueryDTO);
        Page<DishVO> page = (Page<DishVO>) list;
        return new PageResult(page.getTotal(),list);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        for (int i = 0; i<ids.length; i++){
            Dish dish = dishMapper.selectById(ids[i]);
            if (dish.getStatus() == 1) throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        List<Long> stemenl = setmealDishMapper.selectByDishIds(ids);
        if (!stemenl.isEmpty()) throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);

        dishFlavorsMapper.deleteFlavorsByDishIds(ids);

        dishMapper.deleteByIds(ids);
    }

    @Override
    public DishVO selectByIdWithFlavors(Integer id) {
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dishMapper.selectById(id),dishVO);
        List<DishFlavor> list = dishFlavorsMapper.selectByDishId(id);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        if (dishDTO.getFlavors().isEmpty()) return;
        dishFlavorsMapper.deleteFlavorsByDishIds(new Integer[]{dish.getId().intValue()});
        dishFlavorsMapper.saveFlavors(dishDTO);
    }

    @Override
    public List<DishVO> selectBYCategoryId(Integer id) {
        Dish dish = Dish.builder().id(id.longValue()).status(StatusConstant.ENABLE).build();
        List<DishVO> list = dishMapper.selectByCategoryId(dish);
        for (int i= 0;i<list.size();i++){
            DishVO dishVO = list.get(i);
            List<DishFlavor> dishFlavorList = dishFlavorsMapper.selectByDishId(dishVO.getId().intValue());
            dishVO.setFlavors(dishFlavorList);
            list.set(i,dishVO);
        }
        return list;

    }

    @Override
    public void updateStatus(Integer status, Integer id) {
        Dish dish = Dish.builder().status(status).id(id.longValue()).build();
        dishMapper.update(dish);
    }



}
